package com.hyfrogx.modules.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.manage.dao.HyFlashTypeDao;

import com.hyfrogx.modules.manage.entity.HyFlashTypeEntity;
import com.hyfrogx.modules.manage.form.flash.FlashTypeForm;
import com.hyfrogx.modules.manage.service.HyFlashTypeService;

import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0.0
 * @Author: lfh
 * @Date: 2019/9/26 10:09
 * @Company 汉咏科技
 */
@Service
public class HyFlashTypeServiceImpl extends ServiceImpl<HyFlashTypeDao, HyFlashTypeEntity> implements HyFlashTypeService {


    @Override
    public IPage<HyFlashTypeEntity> getByPage(Map map) {
        QueryExt<HyFlashTypeEntity> queryExt = new QueryExt<HyFlashTypeEntity>(map);
        Map qmap = new HashMap();
        qmap.putAll(queryExt.getCondition());
        List<HyFlashTypeEntity> list = this.baseMapper.getFlashTypeByPage(queryExt,map);
        queryExt.setRecords(list);
        return queryExt;
    }

    @Override
    public Boolean saveFlash(FlashTypeForm flashTypeForm) {
        HyFlashTypeEntity shortFlashType = this.getOne(new QueryWrapper<HyFlashTypeEntity>().eq("type_name",flashTypeForm.getTypeName()));
        if(ObjectUtil.isNotNull(shortFlashType))
        {
            throw new MyException("类型重复");
        }
        HyFlashTypeEntity hyFlashTypeEntity = BeanUtil.toBean(flashTypeForm,HyFlashTypeEntity.class);
        hyFlashTypeEntity.setCreatetime(new Date());
        return this.save(hyFlashTypeEntity);
    }

    @Override
    public Boolean updateFlash(FlashTypeForm flashTypeForm) {
        //通过id查询 快讯信息
        HyFlashTypeEntity hyFlashTypeEntity = this.getById(flashTypeForm.getId());
        //非空判断
        Assert.notNull(hyFlashTypeEntity,"快讯类型不存在");
        //是否重复判断
        HyFlashTypeEntity shrotFlashType = this.getOne(new QueryWrapper<HyFlashTypeEntity>().eq("type_name",flashTypeForm.getTypeName()).ne("id",flashTypeForm.getId()));
        if(ObjectUtil.isNotNull(shrotFlashType))
        {
            throw new MyException("类型重复");
        }
        //对象复制
        BeanUtil.copyProperties(flashTypeForm,hyFlashTypeEntity);

        return this.updateById(hyFlashTypeEntity);
    }

    @Override
    public Boolean updateStatus(Integer id) {
        Integer status = this.baseMapper.selcetFlashTStaus(id);
        Assert.notNull(status,"快讯状态为空");
        //判断原状态，赋值与原状态相反的代表数
       status = (status.equals(FrogConstant.FlashType.RELEASE.getValue()))
               ? FrogConstant.FlashType.NORELEASE.getValue():FrogConstant.FlashType.RELEASE.getValue();
       HyFlashTypeEntity hyFlashTypeEntity = new HyFlashTypeEntity();
       hyFlashTypeEntity.setId(id);
       hyFlashTypeEntity.setStatus(status);

       return this.updateById(hyFlashTypeEntity);
    }

}
